package Ligacao;

import java.net.InetAddress;
import java.net.DatagramPacket;

/**
 * Created by IntelliJ IDEA.
 * User: nelson
 */
public class Datagram {
    
    private InetAddress ip;
    private int port;
    private byte[] data;
    private byte[] checksum;


    public Datagram(InetAddress ip, int port, byte[] d, int tamanho)
    {
        int i;
        data = new byte[tamanho];
        this.ip = ip;
        this.port = port;
        for(i=0;i<tamanho;i++)
            data[i] = d[i];
        checksum = new byte[2];
        //createCheckSum()

    }


    public Datagram(DatagramPacket packet){

        checksum = new byte[2];
        ip = packet.getAddress();
        port = packet.getPort();
        int i,j;
        byte[] aux = packet.getData();


        int size = (((aux[0]) & 0x00FF) << 8) + (aux[1] & 0x00FF);

        checksum[0] = aux[2];
        checksum[1] = aux[3];

        data = new byte[size];

        for(i = 0;  i<size; i++)
            data[i] = aux[i+4];

    }


    public InetAddress getIp(){
        return ip;
    }

    public int getPort(){
        return port;
    }

    public byte[] getData(){
        return data;
    }

    public byte[] getCheckSum(){
        return checksum;
    }


    public void setIp(InetAddress ip){
        this.ip = ip;
    }

    public void setPort(int port){
        this.port = port;
    }

    public void setData(byte[] data){
        this.data = data;
    }

    public void setCheckSum(byte[] checksum){
        this.checksum = checksum;
    }



    public byte[] checksum16(byte data[], int offset, int length){
        int value = 0;
        byte checksum[] = new byte[2];

        for(int i = offset; i < length + offset; i += 2){
            value += data[i] << 8;
            if(i + 1 < length + offset) value += data[i+1];
        }

        value = ((value & 0xFFFF0000) >> 16) + (value & 0x0000FFFF);

        checksum[0] = (byte) ((value & 0xFF00) >> 8);
        checksum[1] = (byte) (value & 0xFF);
        return checksum;
    }


    //metodo que coloca o checksum e os dados num array de bytes para que posso ser encapsuldado no campo data do DatagramPacket
    public byte[] toBytes(){

        byte[] result = new byte[4+data.length];

        result[0] = (byte)((data.length & 0xFF00) >> 8);
        result[1] = (byte)((data.length & 0x00FF));

        result[2] = checksum[0];
        result[3] = checksum[1];

        for(int i = 0; i<data.length; i++)
            result[i+4] = data[i];

        return result;
    }

    public byte[] intIn2bytes(int num){

        byte[] byteArray = new byte[2];

        byteArray[0] = (byte)((num & 0xFF00) >> 8);
        byteArray[1] = (byte)((num & 0x00FF));


        return (byteArray);
    }

    public void estragaByte(){

        Integer num1,num2;
        byte b = this.data[0];
        byte b1 = this.data[1];

        num1 = new Integer(b);
        num2 = new Integer(b1);

        if(num1 > 64)
            num1 = num1+ 1;
        else
            num1 = num1 - 1;

        this.data[0] = num1.byteValue();

        if(num2 > 64)
            num2 = num2+ 2;
        else
            num2 = num2 - 2;

        this.data[1] = num2.byteValue();
    }

}
